<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Config;
use App\Models\Customer;
use App\Models\User;
use App\Services\Qiniuyun;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Webpatser\Uuid\Uuid;
class CustomerController extends Controller
{

    public function index(Request $request)
    {
        if (!isset($request->uuid)) {
            $this->code = 1;
            $this->msg = '参数错误';
            return $this->json();
        }
        $tips = Config::where('name' ,'index_tips')->select('value')->first();
        if($tips){
            $tipsData = explode('|' ,$tips->value);
            $this->data['tips'] = $tipsData;
        }
        $bannerData = Banner::where('status' ,1)->select('name','preview','url')->get()->toArray();
        if($bannerData){
            foreach ($bannerData as $banner){
                $this->data['banner'][] = [
                    'name' => $banner['name'],
                    'preview' => Qiniuyun::getFile($banner['preview']),
                    'url' => $banner['url'],
                ];
            }
        }
        return $this->json();

    }
    /**
     * 图片上传
     * @param Request $request
     * @return \App\Http\Controllers\json|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
     */
    public function upUser(Request $request)
    {
        if (!isset($request->uuid)) {
            $this->code = 1;
            $this->msg = '参数错误';
            return $this->json();
        }
        $user = Customer::where('uuid' ,$request->uuid)->first();
        if(!$user){
            $this->code = 1;
            $this->msg = '用户不存在';
            return $this->json();
        }
        if($request->preview){
            $user->preview = $request->preview;
        }
        if(isset($request->nickname) && !empty($request->nickname)){
            $user->nickname = $request->nickname;
        }
        if(isset($request->mobile) && !empty($request->mobile)){
            $user->mobile = $request->mobile;
        }
        $user->save();
        $this->data = [
            'uuid' => $user->uuid,
            'mobile' => $user->mobile,
            'nickname' => strval($user->nickname),
            'preview' => Qiniuyun::getFile($user->preview),
            'username' => strval($user->username),
            'balance' => intval($user->balance),
            'last_login_time' => strval($user->last_login_time),
        ];
        return $this->json();

    }

    /**
     * 用户登录
     * @param Request $request
     * @return \App\Http\Controllers\json
     * @throws \Exception
     */
    public function toLogin(Request $request)
    {
        if (!isset($request->mobile) || !isset($request->password)) {
            $this->code = 1;
            $this->msg = '手机号码或验证码不能为空';
            return $this->json();
        }
        //验证手机号验证码
//        $helper = new SmsService();
////        if (!$helper->checkCode($request->mobile ,$request->code)) {
////            $this->code = 1;
////            $this->msg = '验证码错误';
////            return $this->json();
////        }
        $user = Customer::where('mobile', $request->mobile)->first();
        //没有就注册
        if (!$user) {
            $user = new User();
            $user->uuid = UUID::generate()->uuid_ordered;
            $user->mobile = $request->mobile;
            $user->username = $request->mobile;
            $user->last_login_time = date('Y-m-d H:i:s');
            $user->login_num = 1;
            $user->password = md5(md5($request->password));
            $user->save();
        }else{
            if($user->password !== md5(md5($request->password))){
                $this->code = 1;
                $this->msg = '密码错误';
                return $this->json();
            }
        }
        //返回用户信息
        $this->data = [
            'uuid' => $user->uuid,
            'mobile' => $user->mobile,
            'nickname' => strval($user->nickname),
            'preview' => Qiniuyun::getFile($user->preview),
            'username' => strval($user->username),
            'balance' => intval($user->balance),
            'last_login_time' => strval($user->last_login_time),
        ];
        return $this->json();
    }

    public function getUser(Request $request)
    {
        if (!isset($request->uuid) || empty($request->uuid)) {
            $this->code = 1;
            $this->msg = 'uuid不能为空';
            return $this->json();
        }
        $user = Customer::where('uuid', $request->uuid)->first();
        $this->data = [
            'uuid' => $user->uuid,
            'mobile' => $user->mobile,
            'nickname' => strval($user->nickname),
            'preview' => Qiniuyun::getFile($user->preview),
            'username' => strval($user->username),
            'balance' => intval($user->balance),
            'last_login_time' => strval($user->last_login_time),
        ];
        return $this->json();
    }

    /**
     * 发送短信验证码
     * @param Request $request
     * @return \App\Http\Controllers\json
     */
    public function mobileCode(Request $request)
    {
        if (!isset($request->mobile)) {
            $this->code = 1;
            $this->msg = '手机号码不能为空';
            return $this->json();
        }
        $helper = new SmsService();
        $data = $helper->mobileCode($request->mobile);
        //发送成功
        if($data === true){
            return $this->json();
        }
        $this->code = 1;
        $this->msg = $data;
        return $this->json();
    }

}
